package com.link510.aitools.framework.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.common.base.Strings;
import com.link510.aitools.R;
import com.link510.aitools.core.domain.device.DeviceInfo;

import java.util.List;

import lombok.Data;

public abstract class DeviceAdapter extends BaseAdapter {

    RequestOptions requestOptions = new RequestOptions()
            .circleCrop()
            .placeholder(R.mipmap.device)
            .error(R.mipmap.device);

    private Context mContext;

    /**
     * 获取设备列表
     *
     * @return
     */
    protected abstract List<DeviceInfo> getDeviceInfoList();


    /**
     * 执行命令
     *
     * @param deviceInfo 设备编号
     */
    protected abstract void toAction(DeviceInfo deviceInfo);


    private LayoutInflater mInflator;

    public DeviceAdapter(Context context) {

        this.mContext = context;
        mInflator = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return getDeviceInfoList().size();
    }

    @Override
    public Object getItem(int position) {
        return getDeviceInfoList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint({"ViewHolder", "InflateParams", "SetTextI18n"})
    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {


        final DeviceInfo deviceInfo = getDeviceInfoList().get(position);

        if (deviceInfo == null) {
            return convertView;
        }

        ViewHolder holder = null;

        if (convertView == null) {
            convertView = mInflator.inflate(R.layout.item_device, null);

            holder = new ViewHolder();

            holder.litpicImageView = (ImageView) convertView.findViewById(R.id.device_litpic_imageview);
            holder.titleTextView = (TextView) convertView.findViewById(R.id.device_title_textview);
            holder.descTextView = (TextView) convertView.findViewById(R.id.device_desc_textview);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        if (!Strings.isNullOrEmpty(deviceInfo.getLitpic())) {
            Glide.with(mContext).load(deviceInfo.getLitpic()).apply(requestOptions).into(holder.litpicImageView);
        }

        holder.titleTextView.setText(deviceInfo.getName() + "(" + deviceInfo.getDeviceSN() + ")");
        holder.descTextView.setText(deviceInfo.getDescription());

        convertView.setOnClickListener(v -> {
            toAction(deviceInfo);
        });

        return convertView;
    }

    @Data
    static class ViewHolder {

        /**
         * 图片视图
         */
        ImageView litpicImageView;

        /**
         * 标题视图
         */
        TextView titleTextView;

        /**
         * 描述视图
         */
        TextView descTextView;

    }

}
